--拨锋
--出牌阶段限一次，你可以移动或重铸场上一张牌，令失去牌的角色视为使用目标由你指定的【决斗】。若此牌未对获得牌的角色造成伤害，输者视为对你使用【决斗】。
local bofeng = fk.CreateSkill{
  name = "doublez__bofeng"
}

Fk:loadTranslationTable{
  ["doublez__bofeng"] = "拨锋",
  [":doublez__bofeng"] = "出牌阶段限一次，你可以移动或重铸场上一张牌，令失去牌的角色视为使用目标由你指定的【决斗】。"..
    "若此牌未对获得牌的角色造成伤害，输者视为对你使用【决斗】。",

  ["doublez__bofeng_move"] = "移动",
  ["Recast"] = "重铸",
  ["#doublez__bofeng_discard"] = "拨锋：你可以令 %dest 重铸一张牌",
  ["#doublez__bofeng_choose2duel"] = "拨锋：令 %dest 视为对一名角色使用【决斗】",

  ["$doublez__bofeng1"] = "此时不战，更待何时？",
  ["$doublez__bofeng2"] = "箭在弦上，不得不发。",
}

bofeng:addEffect("active", {
  can_use = function (self, player)
    if player:usedSkillTimes(bofeng.name, player.HistoryPhase) > 0 then return end
    local targets = table.filter(Fk:currentRoom().alive_players, function (player)
      return #player:getCardIds("ej") > 0
    end)
    return #targets > 0
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    if self.interaction.data == "Recast" then
      return #selected == 0 and #to_select:getCardIds("ej") > 0
    else
      if #selected == 0 then
        return true
      elseif #selected == 1 then
        return to_select:canMoveCardsInBoardTo(selected[1]) or selected[1]:canMoveCardsInBoardTo(to_select)
      end
    end
    return
  end,
  card_filter = Util.FalseFunc,
  anim_type = "control",
  interaction = function (self, player)
    return UI.ComboBox{
      choices = {"Recast",
      "doublez__bofeng_move"}
    }
  end,
  feasible = function (self, player, selected, selected_cards, card)
    if self.interaction.data ==  "Recast" then
      return #selected == 1
    else
      return #selected == 2
    end
  end,
  on_use = function (self, room, effect)
    local target = effect.tos[1]
    local player = effect.from
    if self.interaction.data == "Recast" then
      local c = room:askToChooseCard(effect.from, {
        target = target,
        flag = "ej",
        skill_name = bofeng.name,
        prompt = "#doublez__bofeng_discard::"..target.id,
      })
      room:recastCard({c}, target, self.name) 
      if player.dead or target.dead then return end
      local c = Fk:cloneCard("duel")
      c.skillName = "doublez__bofeng"
      local tos = table.filter(room:getAlivePlayers(), function (p)
        return target:canUseTo(c, p)
      end)
      if #tos == 0 then return end
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        cancelable = false,
        skill_name = bofeng.name,
        no_indicate = true,
        prompt = "#doublez__bofeng_choose2duel::"..target.id,
      })
      local use = { ---@type UseCardDataSpec
        card = c,
        from = target,
        tos = to,
      }
      room:useCard(use)
      if player.dead or target.dead or to[1] == player then return end
      if use.damageDealt == nil or use.damageDealt[target] == nil or use.damageDealt[target] == 0 then
        c = Fk:cloneCard("duel")
        c.skillName = "doublez__bofeng"
        room:delay(200)
        local use = { ---@type UseCardDataSpec
          card = c,
          from = to[1],
          tos = {player},
        }
        room:useCard(use)
      end
    else
      local dat = room:askToMoveCardInBoard(player,{
        target_one = effect.tos[1],
        target_two = effect.tos[2],
        flag = nil,
        skill_name = self.name,
      })
      local from = dat.from
      local to = dat.to
      if player.dead or from.dead then return end
      local c = Fk:cloneCard("duel")
      c.skillName = "doublez__bofeng"
      local duel_tos = table.filter (room:getAlivePlayers(), function (p)
        return from:canUseTo(c, p)
      end)
      local duel_to = room:askToChoosePlayers(player, {
        targets = duel_tos,
        min_num = 1,
        max_num = 1,
        cancelable = false,
        skill_name = bofeng.name,
        no_indicate = true,
        prompt = "#doublez__bofeng_choose2duel::"..from.id,
      })
      local use = { ---@type UseCardDataSpec
        card = c,
        from = from,
        tos = duel_to,
      }
      room:useCard(use)
      if player.dead or to.dead then return end
      if use.damageDealt == nil then return end
      local loser ---@type ServerPlayer
      if use.damageDealt[from] then
        loser = from
      else
        loser = duel_to[1]
      end
      if loser == player then return end
      c = Fk:cloneCard("duel")
      c.skillName = "doublez__bofeng"
      room:delay(200)
      local use = { ---@type UseCardDataSpec
        card = c,
        from = loser,
        tos = {player},
      }
      room:useCard(use)
    end
  end
})

return bofeng